/**
 
 * 
 */
package nl.boektrust.btsoep.server;

import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.boektrust.btsoep.shared.model.BTEntity;
import nl.boektrust.btsoep.shared.model.Book;
import nl.boektrust.btsoep.shared.model.BookManifestation;
import nl.boektrust.btsoep.shared.model.Person;
import nl.boektrust.btsoep.shared.model.PhysicalBookManifestation;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.googlecode.objectify.ObjectifyService;

/**
 * @author rao3
 *
 * the implementation of the server that can serve XML requests to Koppelvlak bookstores. 
 */
public class DatabaseRunner extends HttpServlet {
	private boolean isObjectifyServiceRegistered = false;
	private ServletContext context;

	
	ServletContext getContext() {
		return this.context;
	}

	public void init() throws ServletException {
		context = getServletContext();
		//System.err.println("Real Path = " + context.getRealPath("/"));
	}

	/**
	 * 
	 */
	public DatabaseRunner() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param delegate
	 */
	public DatabaseRunner(Object delegate) {
		//super(delegate);
		// TODO Auto-generated constructor stub
	}
	
	private void registerServices () {
		if (isObjectifyServiceRegistered == false) {

			ObjectifyService.register(BTEntity.class);
			ObjectifyService.register(Person.class);
			ObjectifyService.register(PhysicalBookManifestation.class);
			ObjectifyService.register(BookManifestation.class);
			ObjectifyService.register(Book.class);
			isObjectifyServiceRegistered = true;
		}
	}
	
	public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException, IOException  
	{
		
		/*
		 * get raw XML from the inputstream. 
		 */
		
		System.err.println("databaserunner:doPost");

	} 
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		System.err.println("databaserunner:doGet");
		List<PhysicalBookManifestation> books = null;
		int numEntities = new PersistentServiceImpl().numPhysicalBookManifestations();
		Queue queue = QueueFactory.getDefaultQueue();
		try {
			for (int i=0; i<numEntities/500; i++) {
			    queue.add(withUrl("/chunktask").param("key", new Integer(i).toString()));

			}
		}  catch (com.google.apphosting.api.DeadlineExceededException e) {
			System.err.println("Error retrieving books: DeadlineExceededException");

		}
		if (books != null) {
			System.err.println("retrieved "+books.size()+" PhysicalBookManifestation objects");
			for (PhysicalBookManifestation b : books) {
				System.err.print(b.getRecordReference());
				System.err.print(" ");
				new PersistentServiceImpl().deletePhysicalBookManifestation(b);
				new PersistentServiceImpl().persistPhysicalBookManifestation(b);
			}
			System.err.println("");
		}
		System.err.println("databaserunner:doGet finished");
		
	}


}
